<?php
// +----------------------------------------------------------------------
// | Fanwe 方维订餐小秘书商业系统
// +----------------------------------------------------------------------
// | Copyright (c) 2011 http://www.fanwe.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 云淡风轻(88522820@qq.com)
// +----------------------------------------------------------------------

class smenuModule extends StoreBaseModule
{
	public function index(){
		$store_info = $GLOBALS['db']->getRowCached("select name from ".DB_PREFIX."supplier_location where id = ".intval($_REQUEST['sid'])." and is_effect = 1");
		if(!$store_info)
			exit();
		$mid = intval($_REQUEST['mid']);
		if($mid > 0){
			$menu = $GLOBALS['db']->getOne("SELECT cache_data FROM ".DB_PREFIX."user_fav_menu WHERE id=".intval($_REQUEST['mid'])." AND user_id=".intval($GLOBALS['user_info']['id']));
			if($menu)
				$menu = unserialize($menu);
		}
		
		$templist = $GLOBALS['db']->getAllCached("SELECT id,name,price,is_special,cate_id FROM ".DB_PREFIX."supplier_location_menu WHERE supplier_location_id=".intval($_REQUEST['sid'])." ORDER BY id DESC");
		
		$list = array();
				
		foreach($templist as $k=>$v){
			$nums = 0;
			if($mid > 0){
				foreach($menu as $kk=>$vv)
				{
					if($vv['id']==$v['id']){
						$nums = $vv['nums'];
					}
				}
			}
			else{
				$nums = es_session::get("cho_nums_".$v['id']);
			}
			
			if(intval($nums) > 0){
				$templist[$k]['nums'] = $nums;
				$list[]=$templist[$k];
			}
		}
		
		$GLOBALS['tmpl']->assign("store_info",$store_info);
		$GLOBALS['tmpl']->assign("list",$list);
		$GLOBALS['tmpl']->assign("sid",intval($_REQUEST['sid']));
		$GLOBALS['tmpl']->assign("mid",intval($_REQUEST['mid']));
		$GLOBALS['tmpl']->display("store_menu_sms.html");
	}
	
	public function send(){
		convert_req($_REQUEST);	
		
		$return = array("status"=>0,"message"=>'');
		if(!check_ipop_limit(get_client_ip(),"store_smenu",intval(app_conf("SUBMIT_DELAY")))){
			ajax_return(array("status"=>0,"message"=>$GLOBALS['lang']['SUBMIT_TOO_FAST']));
		}
		
		$verify = md5(trim($_REQUEST['code']));
		$session_verify = es_session::get('smenu_sms_verify');
		if($verify!=$session_verify)
		{	
			$return['status'] = 0;
			$return['message'] =$GLOBALS['lang']['VERIFY_CODE_ERROR'];
			ajax_return($return);
		}
		es_session::delete("smenu_sms_verify");
		
		$orderMyTel = trim($_REQUEST['mobile']);
		if(empty($orderMyTel))
		{
			$return['status'] = 0;
			$return['message'] = "您好，我们需要知道您的手机号以方便联系您!";
			ajax_return($return);
		}
		elseif(!check_mobile($orderMyTel)){
			$return['status'] = 0;
			$return['message'] = "您好，您的手机号格式不正确!";
			ajax_return($return);
		}
		
		$store_info = $GLOBALS['db']->getRowCached("select name from ".DB_PREFIX."supplier_location where id = ".intval($_REQUEST['sid'])." and is_effect = 1");
		if(!$store_info){
			$return['status'] = 0;
			$return['message'] = "发送失败，请联系管理员!";
			ajax_return($return);
		}
		$mid = intval($_REQUEST['mid']);
		if($mid > 0){
			$menu = $GLOBALS['db']->getOne("SELECT cache_data FROM ".DB_PREFIX."user_fav_menu WHERE id=".intval($_REQUEST['mid'])." AND user_id=".intval($GLOBALS['user_info']['id']));
			if($menu)
				$menu = unserialize($menu);
		}
		
		$templist = $GLOBALS['db']->getAllCached("SELECT id,name,price,is_special,cate_id FROM ".DB_PREFIX."supplier_location_menu WHERE supplier_location_id=".intval($_REQUEST['sid'])." ORDER BY id DESC");
		
		$content = $store_info['name']."：";
		$tcontent = "";
		$idx = 0;
		foreach($templist as $k=>$v){
			$nums = 0;
			if($mid > 0){
				foreach($menu as $kk=>$vv)
				{
					if($vv['id']==$v['id']){
						$nums = $vv['nums'];
					}
				}
			}
			else{
				$nums = es_session::get("cho_nums_".$v['id']);
			}
			
			if(intval($nums) > 0){
				if($idx != 0)
					$tcontent .="，";
				$tcontent .= $v['name'];
				++$idx;
			}
		}
		if(!empty($tcontent))
		{
			$content .=$tcontent;
			
			$msg_data['send_type'] = 0;
			$msg_data['content'] = addslashes($content);
			$msg_data['send_time'] = 0;
			$msg_data['is_send'] = 0;
			$msg_data['create_time'] = get_gmtime();
			$msg_data['user_id'] = intval($GLOBALS['user_info']['id']);
			$msg_data['is_html'] = 0;
			$msg_data['is_youhui'] = 2;
			$msg_data['dest'] = $orderMyTel;
			$GLOBALS['db']->autoExecute(DB_PREFIX."deal_msg_list",$msg_data,"INSERT"); //插入 
			$insert_id = $GLOBALS['db']->insert_id();
			if($insert_id > 0)
			{
				if($mid==0){
					foreach($templist as $k=>$v){
						es_session::delete("cho_nums_".$v['id']);
					}
				}
			}
			
			$return['status'] = 1;
			ajax_return($return);
		}
		else{
			$return['status'] = 0;
			$return['message'] = "发送失败，请联系管理员!";
			ajax_return($return);
		}
	}
}
?>